package com.aierjun.lgmusic.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.aierjun.lgmusic.R;
import com.aierjun.lgmusic.activity.LoginActivity;
import com.aierjun.lgmusic.adapter.NewsAdapter;
import com.aierjun.lgmusic.entity.NewEntity;
import com.aierjun.lgmusic.utils.TimeUtil;
import com.sam.ui.base.fragment.BaseFragment;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/10.
 */
public class NewsFragment extends BaseFragment{
    private ListView listView;
    private Button btn;
    private List<NewEntity> newEntityList = new ArrayList<>();
    private NewEntity newEntity;
    private OkHttpClient client=new OkHttpClient();
    private Handler handler;

    private Context context;
    private NewsFragment(Context context) {
        this.context=context;
    }


    private static NewsFragment self;
    public synchronized static NewsFragment getInstance(Context context){
        if (self==null) self=new NewsFragment(context);
        return self;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic);
        findView();
        initView();
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what==0){
                    initView();
                }
            }
        };
    }

    @Override
    protected void findView() {
        listView= (ListView) findViewById(R.id.new_list);
        btn= (Button) findViewById(R.id.go_to);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            getexcute();
                            handler.obtainMessage(0,"ok").sendToTarget();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    protected void initView() {
        NewsAdapter newsAdapter=new NewsAdapter(context,newEntityList);
        listView.setAdapter(newsAdapter);
    }

    private void getexcute() throws IOException {
        Request request=new Request.Builder()
                .url("http://192.168.1.245:8080/music/rankingManage/shareing?token="+LoginActivity.token+"&page=0&size=20&loginname="+LoginActivity.username)
                .build();
        Response response=client.newCall(request).execute();
        InputStream in=response.body().byteStream();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        byte[] buf=new byte[1024];
        int len;
        while((len=in.read(buf))!=-1){
            out.write(buf,0,len);
        }
        newsJsonStr(new String(out.toByteArray()));
    }
    private void newsJsonStr(String objStr){
        try {
            JSONArray result=new JSONArray(objStr);
            if(result.length() == 0) return;
            newEntityList.clear();
            for(int i = 0; i < result.length(); i++){
                JSONObject newsObj = result.optJSONObject(i);
                newEntity = new NewEntity();
                Long date=newsObj.optLong("upload_date");
//                Long t=TimeUtil.getInstance().getTime(date);
                newEntity.setDate(date);
                newEntity.setSong_name(newsObj.optString("song_name"));
                newEntity.setLike_count(newsObj.optInt("like_count"));
                newEntity.setLogin_name(newsObj.optString("song_name"));
                newEntity.setIs_whut_student(newsObj.optInt("comment_count"));
                newEntity.setMusician_name(newsObj.optString("musician_name"));
                newEntity.set
                newEntityList.add(newEntity);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
